package com.chunuo.blogs.controller;

import com.chunuo.blogs.dao.BlogsDao;
import com.chunuo.blogs.entity.Blogs;
import com.chunuo.blogs.entity.BlogsFIling;
import com.chunuo.blogs.entity.Comment;
import com.chunuo.blogs.entity.SearchBean;
import com.chunuo.blogs.factory.MarkdownFactory;
import com.chunuo.blogs.service.BlogsService;
import com.chunuo.blogs.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客接口
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-20 14:26
 */
@RestController
public class BlogsController {

    @Autowired
    private BlogsService blogsService;

    @Autowired
    private CommentService commentService;

    /**
     * 博客多条件获取
     * @param page
     * @param size
     * @param title
     * @param classifyId
     * @param tagId
     * @param recommend
     * @return
     */
    @GetMapping("/blogs")
    public PageInfo findAll(@RequestParam("page") Integer page,
                            @RequestParam("size") Integer size,
                            @RequestParam("title") String title,
                            @RequestParam("classifyId") Integer classifyId,
                            @RequestParam("tagId") Integer tagId,
                            @RequestParam("recommend") Integer recommend) {
        List<Blogs> blogsList = blogsService.findAll(new SearchBean(page,size,title,classifyId,tagId,recommend));
        return new PageInfo(blogsList);
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetMapping("/blogs/{id}")
    public Blogs findById(@PathVariable("id")Integer id){
        return blogsService.findById(id);
    }

    /**
     * 根据id获取详细博客信息
     * @param id
     * @return
     */
    @GetMapping("/blogs/details/{id}")
    public Blogs details(@PathVariable("id") Integer id){
        Blogs blogs = blogsService.findById(id);
        blogs.setCommentList(commentService.findAll(id));
        String content = MarkdownFactory.markdownBasic(blogs.getContent()).replaceAll("&amp;","&");
        System.out.println(content);
        blogs.setContent(content);
        return  blogs;
    }

    /**
     *  根据归档日期查找博客
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/blogs/filing")
    public PageInfo findBlogsFiling(@RequestParam("page")Integer page,@RequestParam("size")Integer size){
        List<BlogsFIling> filing = blogsService.findFiling(page, size);
        System.out.println("blogsService === "+filing);
        return new PageInfo(filing);
    }

    /**
     * 保存博客
     * @param blogs
     * @return
     */
    @PostMapping("/blogs")
    public Integer save(@RequestBody Blogs blogs){
        blogsService.save(blogs);
        return 1;
    }

    /**
     * 更新博客内容
     * @param blogs
     * @return
     */
    @PutMapping("/blogs")
    public Integer update(@RequestBody Blogs blogs){
        System.out.println(blogs.getContent());
        blogsService.update(blogs);
        return 1;
    }

    /**
     * 根据id删除博客
     * @param id
     * @return
     */
    @DeleteMapping("/blogs/{id}")
    public Integer delete(@PathVariable("id")Integer id){
        blogsService.delete(id);
        return 1;
    }

}
